function solution(bridge_length, weight, truck_weights) {
let tick = 0
let currentBridgeWeight = 0
const waitingTrucks = [...truck_weights]
const passingTrucks = []
const passedTrucks = []
while (passedTrucks.length !== truck_weights.length) {
tick += 1
if (
passingTrucks.length > 0 &&
tick - passingTrucks[0].tick === bridge_length
) {
const { truck: passedTruck } = passingTrucks.shift()
currentBridgeWeight -= passedTruck
passedTrucks.push(passedTruck)
}
if (
waitingTrucks.length > 0 &&
currentBridgeWeight + waitingTrucks[0] <= weight
) {
const truck = waitingTrucks.shift()
currentBridgeWeight += truck
passingTrucks.push({ truck, tick })
}
}
return tick
}